package controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import pojo.Comment;
import pojo.User;
import  pojo.Essay;
import pojo.EssayAndUser;
import pojo.EssayAndUserAndComment;
import service.CommentServiceI;
import service.EssayAndUserAndCommentServiceI;
import service.EssayServiceI;
import service.MessageServiceI;

@Controller
@RequestMapping("comment")
public class CommentController {

	@Autowired 
	private EssayAndUserAndCommentServiceI commentServiceI;
	@Autowired
	private MessageServiceI messageServiceI;
	/**
	 * 
	 * @Title: showcomment   
	 * @Description: 显示评论
	 * @param: @param request
	 * @param: @param modelAndView
	 * @param: @param eUser
	 * @param: @param essayId
	 * @param: @return      
	 * @return: ModelAndView      
	 * @throws
	 */
	@RequestMapping("showcomment")
	public ModelAndView showcomment(HttpServletRequest request,ModelAndView modelAndView,
			@ModelAttribute(value="essay") EssayAndUser eUser,@ModelAttribute(value="essayId")Integer essayId){
		List<EssayAndUserAndComment> list=commentServiceI.FindAllCommentByEssayId(essayId);
		modelAndView.addObject("essay", eUser);
		modelAndView.addObject("commentlist", list);
		modelAndView.setViewName("/community/jsp/essay/essaydetail");
		return modelAndView;
		
	}
	
	@Autowired
	CommentServiceI cServiceI;
	@Autowired
	EssayServiceI eServiceI;
	/**
	 * 
	 * @Title: comment   
	 * @Description: 发表评论
	 * @param: @param request
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("publishcomment")
	@ResponseBody
	public String comment(HttpServletRequest request) {
		Integer essayId = Integer.valueOf(request.getParameter("essayId")).intValue();
		String comment=request.getParameter("content");
		User user=(User) request.getSession().getAttribute("user");
		if(user==null) {
			return "unlogin";
		}
		Integer userId=user.getUserId();
		Comment comment2=new Comment();
		comment2.setEssayId(essayId);
		comment2.setUserId(userId);
		comment2.setComment(comment);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// �������ڸ�ʽ
		String commentdate = df.format(new Date());// 获取当前时间
		comment2.setCommentDate(commentdate);
		int j=cServiceI.insertcomment(comment2);
		/***************更新评论数量********************/
		Essay essay=eServiceI.FindEssay(essayId);
		int temp=essay.getCommentNumber();
		temp++;
		Essay essay2=new Essay();
		essay2.setEssayId(essayId);
		essay2.setCommentNumber(temp);
		int k=eServiceI.UpdateEssayPtcvr(essay2);
		if(j>0&&k>0) {
			/*********************进行消息表的插入数据*****************************/
			
			Integer senduser=userId;
			Integer receiveuser=essay.getUserId();
			Integer operateflag=2;
			try {
				messageServiceI.MakeMessage(senduser, receiveuser, operateflag, essayId);
			} catch (Exception e) {
				e.printStackTrace();
				return "false";
			}
			/*******************************消息表更新完成******************************************************/
			return "success";
		}else
			return "false";
	}
	
}
